package util;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map.Entry;
import java.util.SortedMap;
import java.util.TreeMap;

import javax.swing.JFrame;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class GraficoLinha extends JFrame {
	private static final long serialVersionUID = -6961625310907910901L;

	public GraficoLinha(String tituloAplicacao, String tituloGrafico, List<List<Double>> data){
		super(tituloAplicacao);
			
		XYDataset dataset = fetchDataset(tituloGrafico, data);
		JFreeChart chart = createChart(dataset, tituloGrafico);
		ChartPanel chartPanel = new ChartPanel(chart);
		
		
		/*NumberAxis xAxis = (NumberAxis) chart.getXYPlot().getDomainAxis();
		xAxis.setTickUnit(new NumberTickUnit(3.0));*/
		
		//Graduando o eixo Y de 0.1 em 0.1
		ValueAxis yAxis = (NumberAxis) chart.getXYPlot().getRangeAxis();
		((NumberAxis)yAxis).setTickUnit(new NumberTickUnit(0.1));
		
		chartPanel.setPreferredSize(new java.awt.Dimension(500,270));
		setContentPane(chartPanel);
		
	}
	
	private JFreeChart createChart(XYDataset dataset, String titulo) {
		JFreeChart chart = ChartFactory.createXYLineChart(
				titulo,
				"Tempo no Sistema - x",
				"P(X < x)",
				dataset,
				PlotOrientation.VERTICAL,
				true,
				true,
				false
				);
		
		chart.setBackgroundPaint(Color.white);
		
		XYPlot plot = chart.getXYPlot();
		plot.setBackgroundPaint(Color.lightGray);
		plot.setDomainGridlinePaint(Color.white);
		plot.setRangeGridlinePaint(Color.white);
		
		XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
		renderer.setSeriesLinesVisible(0,true);
		renderer.setSeriesShapesVisible(0,false);
		
		NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
		rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		
		
		return chart;
	}

	private XYDataset fetchDataset(String tituloGrafico, List<List<Double>> data){
		List<Double> user_times = new ArrayList<Double>();
		
		XYSeriesCollection dataset = new XYSeriesCollection();
		
		for(int i=0; i < data.size(); i++){
			List<Double> runData; //data for each run of the sim
			runData = data.get(i);
			
			Collections.sort(runData);
			
			SortedMap<Double, Double> map = new TreeMap<Double, Double>();
			for(int j = 1; j <= runData.size(); j++) {
				map.put(runData.get(j - 1), j*1.0/runData.size()*1.0);
				user_times.add(runData.get(j - 1));
			}
			
			XYSeries run_dataset = new XYSeries(tituloGrafico+" - "+i);
			
			for(Entry<Double, Double> e : map.entrySet()) {
				run_dataset.add(e.getKey(), e.getValue());
//				user_times.add(e.getKey());
			}
			
			
			dataset.addSeries(run_dataset);
			Collections.sort(user_times);
			
			if(user_times.size() % 2 == 0){
				System.out.println("Mediana: " + user_times.get( (user_times.size()-1) / 2) );	
			}else{
				System.out.println("Mediana: " + user_times.get( (user_times.size()) / 2) );
			}
		
			
		}
		
		
		
		
		
		
		
		
		//System.out.println("Temos " + dataset_1.getItemCount() + "elementos.");
		
		
		/*dataset_1.add(1.0, 1.0);
        dataset_1.add(2.0, 4.0);
        dataset_1.add(3.0, 3.0);
        dataset_1.add(4.0, 5.0);
        dataset_1.add(5.0, 5.0);
        dataset_1.add(6.0, 7.0);
        dataset_1.add(7.0, 7.0);
        dataset_1.add(8.0, 8.0);*/
        
        
        
        
        return dataset;
	}
	
	
	

}
